<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\model\shiyao\Center as CenterModel;
use app\common\service\store\User as UserService;
use think\facade\Db;

/**
 *
 * Class StoreUser
 * @package app\store\model
 */
class Center extends CenterModel {

	public function getListAll() {
		// 查询模型
		$query = $this->getNewQuery();

		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['create_time' => 'desc'])
			->select()->toArray();

		return $list;
	}

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => []]);
		$filter = [];
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		$store_user = UserService::getLoginInfo();
		if ($store_user['user']['company_id'] > 0) {

			$filter[] = ['p.company_id', 'in', [0, $store_user['user']['company_id']]];
			
			$centerIds = Db::name('shiyao_project_center')->alias('c')
				->join('shiyao_project p', 'p.project_id=c.project_id')
				->where('p.company_id', $store_user['user']['company_id'])
				->field('c.center_id')
				->select()->toArray();
			$centerIds = array_column($centerIds, 'center_id');
			if (empty($centerIds)) {
				//$filter[] = ['p.center_id', '=', -1];
			} else {
				$filter[] = ['p.center_id', 'in', $centerIds];
			}

		}

		// 关键词搜索
		!empty($params['search']) && $query->where('p.center_name', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')
		//->join('shiyao_company c','c.company_id=p.company_id')
			->where('p.is_delete', '=', '0')
			->where($filter)
			->order(['p.address' => 'asc'])
			->paginate(15);

		return $list;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {

		$data['store_id'] = self::$storeId;
		$store_user = UserService::getLoginInfo();

		if ($store_user['user']['company_id'] > 0) {
			$data['company_id'] = $store_user['user']['company_id'];
		}

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {
	
		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	public function audit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {

		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			return $this->save(['is_delete' => 1]);
		});
	}
	public static function detail(int $projectId) {

		$detail = self::get(['center_id' => $projectId]);

		return $detail;
	}

}
